﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using CarLoanSystem.Entity;

namespace CarLoanSystem
{
    public partial class frmCheckPay : Form
    {
        public frmCheckPay()
        {
            InitializeComponent();
            this.lblStatus.Text = "";
            this.lblStatus.Update();
        }

        public void CheckPay()
        {
            DateTime date = DateTime.Now;
            DataTable dt = DAL.DeptInfoDAL.LoanUnPaid(date.Year + "-" + date.Month + "-" + date.Day);
            if (dt == null)
            {
                progressBar.Maximum = 1;
                progressBar.Step = 1;
            }
            else
            {
                progressBar.Maximum = dt.Rows.Count;
                progressBar.Step = 1;
                DeptInfoItem deptItem;
                ScheduleItem scheduleItem;
                LoanItem loanItem;
                EmployeeItem empItem;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //DeptInfoItem
                    deptItem = new DeptInfoItem();
                    deptItem.DEBT_STATUS = "A";
                    deptItem.EMPLOYEE_ID = DAL.ScheduleInfoDAL.LoadEmpJobDept();
                    deptItem.LOAN_ID = Convert.ToInt32(dt.Rows[i]["LOAN_ID"].ToString());
                    DAL.DeptInfoDAL.Insert(deptItem);

                    //ScheduleItem
                    scheduleItem = new ScheduleItem();
                    scheduleItem.EMPLOYEE_ID = deptItem.EMPLOYEE_ID;
                    scheduleItem.LOAN_ID = deptItem.LOAN_ID;
                    scheduleItem.SCHEDULE_JOB = "D";
                    scheduleItem.SCHEDULE_STATUS = "A";
                    DAL.ScheduleInfoDAL.Insert(scheduleItem);

                    loanItem = new LoanItem();
                    loanItem.LOAN_ID = deptItem.LOAN_ID;
                    loanItem.DEBT_STATUS = "A";
                    DAL.LoanInfoDAL.UpdateDeptStatus(loanItem);

                    empItem = DAL.EmployeeInfoDAL.LoadEmployee(deptItem.EMPLOYEE_ID.ToString());

                    AlertsMessageItem alertItem = new AlertsMessageItem();
                    alertItem.ALERTS_MESSAGE_DETAIL = "แจ้งเตือนการทวงหนี้สินของรหัสการขอกู้ " + loanItem.LOAN_ID;
                    alertItem.ALERTS_MESSAGE_STATUS = "A";
                    alertItem.USER_NAME = empItem.USERNAME;
                    alertItem.LOAN_ID = loanItem.LOAN_ID;
                    DAL.AlertsMessageInfoDAL.Insert(alertItem);
                }
            }
            progressBar.PerformStep();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.lblStatus.Text = "กำลังทำงาน";
            this.lblStatus.Update();
            CheckPay();
            this.lblStatus.Text = "ทำงานเสร็จสิ้น";
            this.lblStatus.Update();
        }
    }
}
